import java.util.Calendar;
import java.util.Date;

public class Timer {

	private static boolean isMarketOpen = false;
	private static Date date;

	public static boolean isMarketOpen() {
		return isMarketOpen;
	}

	public void StartStopMSV(boolean result) {
		isMarketOpen = result;
		if (isMarketOpen) {
			new Thread(new Runnable() {
				public void run() {

					while (true) {
						if (isMarketOpen) {
							ActiveOrder activeOrder = new ActiveOrder();
							activeOrder.retrieveOrdersFromRepository();
							MatchStockValue.getMatchStockValue().matchStocks();
						} else {
							MutualFundRepository.getMutualFundRepository()
									.calculateLatestMfPrice();
							ActiveOrder.performOperationsOnMarketClose();
						}
					}
				}
			}).start();
		}

	}

	public void setDate(int timestamp) {
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, timestamp);

		if (calendar.get(Calendar.HOUR_OF_DAY) >= Constants.MARKET_CLOSE_HOUR) {
			calendar.set(Calendar.HOUR_OF_DAY, Constants.MARKET_CLOSE_HOUR);
			calendar.set(Calendar.MINUTE, Constants.MARKET_CLOSE_MINUTE);
			calendar.set(Calendar.SECOND, Constants.MARKET_CLOSE_SECOND);
		}
		Timer.date = calendar.getTime();
	}

	public static Date getDate() {
		return date;
	}
}
